//
//  TGMModel2ViewAdapter.h
//  TravelGuideMdd
//
//  Created by HanFeng on 3/12/14.
//  Copyright (c) 2014 mafengwo.com. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "UIScrollView+TGMPullToRefresh.h"
#import "TGMDefaultActivityPullToRefreshView.h"

@class TGMModel2ScrollViewAdapter;
@class TGMBasePullToRefreshHandler;

@protocol TGMModel2ScrollViewAdapterDelegate <NSObject>

@optional

- (NSDictionary*)adpater:(TGMModel2ScrollViewAdapter*)adapter requestParamemtersForRequestType:(MFWDataRequestType)requestType;

- (void)adapter:(TGMModel2ScrollViewAdapter*)adapter didFinishRequest:(MFWDataRequestType)requestType withError:(NSError*)error;
- (void)adapterDidRefresh:(TGMModel2ScrollViewAdapter*)adapter;

- (void)adapterDidScroll:(TGMModel2ScrollViewAdapter*)adapter;

-(void)onReloadData:(TGMModel2ScrollViewAdapter*)adapter;

- (UIEdgeInsets)adapter:(TGMModel2ScrollViewAdapter*)adapter scrollContentInset:(UICollectionView*)collectionView;

@end

@interface TGMModel2ScrollViewAdapter : NSObject <MFWDataObserver, UIScrollViewDelegate> {
    @protected
    __weak UIScrollView *_scrollView;
    
    TGMBaseModelActionData *_actionData;
    
    int _initOffsetY;
    
    BOOL _refreshLoading;
    BOOL _refreshError;
}

@property (nonatomic, weak, readonly) UIScrollView *scrollView;
@property (nonatomic, weak) id<TGMModel2ScrollViewAdapterDelegate> delegate;
@property (nonatomic, readonly) TGMBasePullToRefreshHandler *pullToRefreshHandler;

#ifdef YWBaseViewController_h
@property(nonatomic, weak)OIBaseViewController   *contextViewController;
#else
@property(nonatomic, weak)UIViewController   *contextViewController;
#endif

#pragma mark - Task

// request action data
@property(nonatomic, copy, readonly) TGMBaseModelActionData *actionData;
- (TGMBaseModelActionData*)actionDataForRequestModelData:(MFWDataRequestType)type;

@property(nonatomic, strong) MFWTask *currentOperationTask;

#pragma mark - Data

- (void)buildListData;

- (void)reloadUIData:(BOOL)refreshed byRequestType:(MFWDataRequestType)type;

#pragma mark - Refresh

- (UIEdgeInsets)contextScrollViewContentInset; // 设置上下文滚动的contentInset

// 请求刷新下拉刷新样式，是否刷新根据实际情况而变
- (void)requirePullToRefreshFacadeStyle;
- (BOOL)asyncRequirePullToRefreshFacadeStyle; // 默认返回YES，异步加载
- (void)cleanPullToRefreshFacadeStyle;

- (void)stopPullToRefreshLoadingAnimating:(BOOL)refreshSuccess; // 重载加载过程的子类只能调用此方法，才能正确停止下拉刷新加载过程

// 返回动态场景下拉刷新的,这个更新后，需要重新请求requirePullToRefreshFacadeStyle，才会生效
// 默认取[self.contextViewController pageName];
- (NSString *)pageNameForPullToRefresh;
- (NSString *)filterKeyForPullToRefresh;

@property(nonatomic, readonly) BOOL isRefreshLoading;
- (void)triggerRefresh; // 出发刷新动作
- (void)cancelRefresh;

- (void)requestRefresh; // 实际刷新动作
- (void)requestGetMore; // 实际加载更多动作
- (MFWTask*)startRefreshRequest; // 产生刷新请求
- (MFWTask*)startGetMoreRequest; // 产生加载更多请求

- (BOOL)supportPullRefreshHeader;//default return NO
- (BOOL)supportGetMoreFooter;//default return NO

#pragma mark - UIScrollViewDelegate Implementations
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;

@end
